<?php
/** Yz_SQL_Statement_Select
*
*/

class Yz_SQL_Statement_Union
    extends Yz_SQL_Statement_Select
{

    const CLAUSE_UNION = 'UNION';

    protected $clauses = array(
        self::CLAUSE_UNION     => '',
        self::CLAUSE_ORDER_BY   => '',
        self::CLAUSE_LIMIT      => '',
        self::CLAUSE_FOR_UPDATE => '',
    );


    /*****************************
     *      Public methods       *
     *****************************/

    /**
     *  @param varargs(Yz_SQL_Statement_Select)
     *  @return Yz_SQL_Statement_Select
     *  @todo such a setup that allows to apply order-by and limit to the UNION
     */
    public function union(Yz_SQL_Statement_Select $st1, Yz_SQL_Statement_Select $st2)
    {
        $unions = array();
        foreach (func_get_args() as $st) {
            assert('$st instanceof Yz_SQL_Statement_Select');
            $unions[] = '(' . $st->getSQL() . ')';
        }
        assert('count($unions) >= 2');
        $this->clauses[self::CLAUSE_UNION] = join("\nUNION\n", $unions);
        return $this;
    }


    /***************************************
     ********** Constructor ****************/


    /** build FROM clause, optionally with joined tables:
     *      accept array of tables, which will be processed by joinTables(),
     *      except the first entry whose key is the alias and value the table name
     *  @param string|array-type $table
     *  @param string|null $alias
     *  @return this
     */
    public function from( $table, $alias=null )
    {
        throw NotImplementedError::_new_();
    }

    public function where( $what )
    {
        throw NotImplementedError::_new_();
    }

    public function orWhere( $what )
    {
        throw NotImplementedError::_new_();
    }

    public function groupBy( $what )
    {
        throw NotImplementedError::_new_();
    }

    public function having( $what )
    {
        throw NotImplementedError::_new_();
    }

    /**
     *  @return string SQL
     */
    protected function isComplete()
    {
        debug();

        $clauses = $this->clauses;

        return  "" !== $clauses[self::CLAUSE_UNION] ;
    } // testComplete


} // class

